package snafu.implementation.actions.collect;

import java.util.List;

import snafu.framework.actions.IntentionItself;
import snafu.framework.actions.IntentionView;
import snafu.framework.agents.AgentState;
import snafu.framework.objects.WorldObject;
import snafu.framework.states.State;
import snafu.framework.view.Access;
import snafu.framework.worlds.Position;
import snafu.framework.worlds.WorldMap;
import snafu.implementation.states.literals.collect.CollectableLiteral;
import snafu.implementation.states.literals.position.PositionLiteral;

public class CollectIntentionItself extends IntentionItself<CollectAction> implements CollectIntention {

	public CollectIntentionItself(Access privacy) {
		super(privacy);
	}

	@Override
	public CollectAction createAction(List<WorldObject> targets) {
		return new CollectAction(targets, this);
	}

	@Override
	public IntentionView<CollectAction> createView(Access access) {
		return new CollectIntentionView(access, this);
	}

	@Override
	protected boolean checkApplicability(WorldMap perception, AgentState agent, List<WorldObject> targets) {
		// can only collect one at a time
		if (targets.size() > 1) {
			return false;
		}
		
		boolean applicable = true;			 
		
		// get the only target and its state
		WorldObject target = targets.get(0);
		if (target == null) {
			return false;
		}
		
		State targetState = target.getState();
		
		// check if the target is collectable
		applicable &= targetState.getLiteral(CollectableLiteral.class, false);
		
		// get the agent's and the target's position
		Position agentPosition = agent.getLiteral(PositionLiteral.class);
		Position targetPosition = perception.getPosition(target);

		// check if they share the same position
		if (agentPosition == null)
			return false;
		applicable &= agentPosition.equals(targetPosition);
		
		return applicable;
	}

}
